﻿<script lang="ts" setup name="incomingorders">
import { ref, reactive, onMounted } from "vue";
import { auth } from '/@/utils/authFunction';
import { ElMessageBox, ElMessage } from "element-plus";
import { downloadStreamFile } from "/@/utils/download";
import { useIncomingordersApi } from '/@/api/IncomingOrdersss/incomingorders';
import editDialog from '/@/views/IncomingOrdersss/incomingorders/component/editDialog.vue'
import printDialog from '/@/views/system/print/component/hiprint/preview.vue'
import ModifyRecord from '/@/components/table/modifyRecord.vue';
import ImportData from "/@/components/table/importData.vue";

const incomingordersApi = useIncomingordersApi();
const printDialogRef = ref();
const editDialogRef = ref();
const importDataRef = ref();
const state = reactive({
  exportLoading: false,
  tableLoading: false,
  stores: {},
  showAdvanceQueryUI: false,
  dropdownData: {} as any,
  selectData: [] as any[],
  tableQueryParams: {} as any,
  tableParams: {
    page: 1,
    pageSize: 20,
    total: 0,
    field: 'createTime', // 默认的排序字段
    order: 'descending', // 排序方向
    descStr: 'descending', // 降序排序的关键字符
  },
  tableData: [],
});

// 页面加载时
onMounted(async () => {
  const data = await incomingordersApi.getDropdownData(true).then(res => res.data.result) ?? {};
  state.dropdownData.vendor = data.vendor;
  state.dropdownData.incomingDepId = data.incomingDepId;
  state.dropdownData.incomingPeopleId = data.incomingPeopleId;
  state.dropdownData.makeSingleId = data.makeSingleId;
  state.dropdownData.depId = data.depId;
  state.dropdownData.operator = data.operator;
});

// 查询操作
const handleQuery = async (params: any = {}) => {
  state.tableLoading = true;
  state.tableParams = Object.assign(state.tableParams, params);
  const result = await incomingordersApi.page(Object.assign(state.tableQueryParams, state.tableParams)).then(res => res.data.result);
  state.tableParams.total = result?.total;
  state.tableData = result?.items ?? [];
  state.tableLoading = false;
};

// 列排序
const sortChange = async (column: any) => {
  state.tableParams.field = column.prop;
  state.tableParams.order = column.order;
  await handleQuery();
};

// 删除
const delIncomingorders = (row: any) => {
  ElMessageBox.confirm(`确定要删除吗?`, "提示", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  }).then(async () => {
    await incomingordersApi.delete({ id: row.id });
    handleQuery();
    ElMessage.success("删除成功");
  }).catch(() => {});
};

// 批量删除
const batchDelIncomingorders = () => {
  ElMessageBox.confirm(`确定要删除${state.selectData.length}条记录吗?`, "提示", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  }).then(async () => {
    await incomingordersApi.batchDelete(state.selectData.map(u => ({ id: u.id }) )).then(res => {
      ElMessage.success(`成功批量删除${res.data.result}条记录`);
      handleQuery();
    });
  }).catch(() => {});
};

// 导出数据
const exportIncomingordersCommand = async (command: string) => {
  try {
    state.exportLoading = true;
    if (command === 'select') {
      const params = Object.assign({}, state.tableQueryParams, state.tableParams, { selectKeyList: state.selectData.map(u => u.id) });
      await incomingordersApi.exportData(params).then(res => downloadStreamFile(res));
    } else if (command === 'current') {
      const params = Object.assign({}, state.tableQueryParams, state.tableParams);
      await incomingordersApi.exportData(params).then(res => downloadStreamFile(res));
    } else if (command === 'all') {
      const params = Object.assign({}, state.tableQueryParams, state.tableParams, { page: 1, pageSize: 99999999 });
      await incomingordersApi.exportData(params).then(res => downloadStreamFile(res));
    }
  } finally {
    state.exportLoading = false;
  }
}

handleQuery();
</script>
<template>
  <div class="incomingorders-container" v-loading="state.exportLoading">
    <el-card shadow="hover" :body-style="{ paddingBottom: '0' }"> 
      <el-form :model="state.tableQueryParams" ref="queryForm" labelWidth="90">
        <el-row>
          <el-col :xs="24" :sm="12" :md="12" :lg="8" :xl="4" class="mb10">
            <el-form-item label="关键字">
              <el-input v-model="state.tableQueryParams.keyword" clearable placeholder="请输入模糊查询关键字"/>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :md="12" :lg="8" :xl="4" class="mb10" v-if="state.showAdvanceQueryUI">
            <el-form-item label="进货单号">
              <el-input v-model="state.tableQueryParams.incomingNumber" clearable placeholder="请输入进货单号"/>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :md="12" :lg="8" :xl="4" class="mb10" v-if="state.showAdvanceQueryUI">
            <el-form-item label="单据状态">
              <el-input v-model="state.tableQueryParams.ordersStatus" clearable placeholder="请输入单据状态"/>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :md="12" :lg="8" :xl="4" class="mb10" v-if="state.showAdvanceQueryUI">
            <el-form-item label="进货日期">
              <el-date-picker type="daterange" v-model="state.tableQueryParams.incomingDataRange"  value-format="YYYY-MM-DD HH:mm:ss" start-placeholder="开始日期" end-placeholder="结束日期" :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]" />
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :md="12" :lg="8" :xl="4" class="mb10" v-if="state.showAdvanceQueryUI">
            <el-form-item label="供应商">
              <el-select clearable filterable v-model="state.tableQueryParams.vendor" placeholder="请选择供应商">
                <el-option v-for="(item,index) in state.dropdownData.vendor ?? []" :key="index" :value="item.value" :label="item.label" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :md="12" :lg="8" :xl="4" class="mb10" v-if="state.showAdvanceQueryUI">
            <el-form-item label="联系人">
              <el-input v-model="state.tableQueryParams.contact" clearable placeholder="请输入联系人"/>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :md="12" :lg="8" :xl="4" class="mb10" v-if="state.showAdvanceQueryUI">
            <el-form-item label="联系方式">
              <el-input v-model="state.tableQueryParams.contactManaer" clearable placeholder="请输入联系方式"/>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :md="12" :lg="8" :xl="4" class="mb10" v-if="state.showAdvanceQueryUI">
            <el-form-item label="进货部门">
              <el-select clearable filterable v-model="state.tableQueryParams.incomingDepId" placeholder="请选择进货部门">
                <el-option v-for="(item,index) in state.dropdownData.incomingDepId ?? []" :key="index" :value="item.value" :label="item.label" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :md="12" :lg="8" :xl="4" class="mb10" v-if="state.showAdvanceQueryUI">
            <el-form-item label="进货人">
              <el-select clearable filterable v-model="state.tableQueryParams.incomingPeopleId" placeholder="请选择进货人">
                <el-option v-for="(item,index) in state.dropdownData.incomingPeopleId ?? []" :key="index" :value="item.value" :label="item.label" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :md="12" :lg="8" :xl="4" class="mb10" v-if="state.showAdvanceQueryUI">
            <el-form-item label="货品数量">
              <el-input-number v-model="state.tableQueryParams.goodsNum"  clearable placeholder="请输入货品数量"/>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :md="12" :lg="8" :xl="4" class="mb10" v-if="state.showAdvanceQueryUI">
            <el-form-item label="货品单价">
              <el-input-number v-model="state.tableQueryParams.goodsPrices"  clearable placeholder="请输入货品单价"/>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :md="12" :lg="8" :xl="4" class="mb10" v-if="state.showAdvanceQueryUI">
            <el-form-item label="进货货品">
              <el-input v-model="state.tableQueryParams.goodsName" clearable placeholder="请输入进货货品"/>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :md="12" :lg="8" :xl="4" class="mb10" v-if="state.showAdvanceQueryUI">
            <el-form-item label="制单人">
              <el-select clearable filterable v-model="state.tableQueryParams.makeSingleId" placeholder="请选择制单人">
                <el-option v-for="(item,index) in state.dropdownData.makeSingleId ?? []" :key="index" :value="item.value" :label="item.label" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :md="12" :lg="8" :xl="4" class="mb10" v-if="state.showAdvanceQueryUI">
            <el-form-item label="所在部门">
              <el-select clearable filterable v-model="state.tableQueryParams.depId" placeholder="请选择所在部门">
                <el-option v-for="(item,index) in state.dropdownData.depId ?? []" :key="index" :value="item.value" :label="item.label" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :md="12" :lg="8" :xl="4" class="mb10" v-if="state.showAdvanceQueryUI">
            <el-form-item label="备注">
              <el-input v-model="state.tableQueryParams.incomingDesc" clearable placeholder="请输入备注"/>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :md="12" :lg="8" :xl="4" class="mb10" v-if="state.showAdvanceQueryUI">
            <el-form-item label="制单时间">
              <el-date-picker type="daterange" v-model="state.tableQueryParams.makeDataRange"  value-format="YYYY-MM-DD HH:mm:ss" start-placeholder="开始日期" end-placeholder="结束日期" :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]" />
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :md="12" :lg="8" :xl="4" class="mb10" v-if="state.showAdvanceQueryUI">
            <el-form-item label="审核时间">
              <el-date-picker type="daterange" v-model="state.tableQueryParams.auditTimeRange"  value-format="YYYY-MM-DD HH:mm:ss" start-placeholder="开始日期" end-placeholder="结束日期" :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]" />
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :md="12" :lg="8" :xl="4" class="mb10" v-if="state.showAdvanceQueryUI">
            <el-form-item label="进货明细">
              <el-input v-model="state.tableQueryParams.organizationalStructureId" clearable placeholder="请输入进货明细"/>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :md="12" :lg="8" :xl="4" class="mb10" v-if="state.showAdvanceQueryUI">
            <el-form-item label="上传附件">
              <el-input v-model="state.tableQueryParams.storageDesc" clearable placeholder="请输入上传附件"/>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :md="12" :lg="8" :xl="4" class="mb10" v-if="state.showAdvanceQueryUI">
            <el-form-item label="操作人">
              <el-select clearable filterable v-model="state.tableQueryParams.operator" placeholder="请选择操作人">
                <el-option v-for="(item,index) in state.dropdownData.operator ?? []" :key="index" :value="item.value" :label="item.label" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :md="12" :lg="8" :xl="4" class="mb10">
            <el-form-item >
              <el-button-group style="display: flex; align-items: center;">
                <el-button type="primary"  icon="ele-Search" @click="handleQuery" v-auth="'incomingorders:page'" v-reclick="1000"> 查询 </el-button>
                <el-button icon="ele-Refresh" @click="() => state.tableQueryParams = {}"> 重置 </el-button>
                <el-button icon="ele-ZoomIn" @click="() => state.showAdvanceQueryUI = true" v-if="!state.showAdvanceQueryUI" style="margin-left:5px;"> 高级查询 </el-button>
                <el-button icon="ele-ZoomOut" @click="() => state.showAdvanceQueryUI = false" v-if="state.showAdvanceQueryUI" style="margin-left:5px;"> 隐藏 </el-button>
                <el-button type="danger" style="margin-left:5px;" icon="ele-Delete" @click="batchDelIncomingorders" :disabled="state.selectData.length == 0" v-auth="'incomingorders:batchDelete'"> 删除 </el-button>
                <el-button type="primary" style="margin-left:5px;" icon="ele-Plus" @click="editDialogRef.openDialog(null, '新增进货订单')" v-auth="'incomingorders:add'"> 新增 </el-button>
                <el-dropdown :show-timeout="70" :hide-timeout="50" @command="exportIncomingordersCommand">
                  <el-button type="primary" style="margin-left:5px;" icon="ele-FolderOpened" v-reclick="20000" v-auth="'incomingorders:export'"> 导出 </el-button>
                  <template #dropdown>
                    <el-dropdown-menu>
                      <el-dropdown-item command="select" :disabled="state.selectData.length == 0">导出选中</el-dropdown-item>
                      <el-dropdown-item command="current">导出本页</el-dropdown-item>
                      <el-dropdown-item command="all">导出全部</el-dropdown-item>
                    </el-dropdown-menu>
                  </template>
                </el-dropdown>
                <el-button type="warning" style="margin-left:5px;" icon="ele-MostlyCloudy" @click="importDataRef.openDialog()" v-auth="'incomingorders:import'"> 导入 </el-button>
              </el-button-group>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </el-card>
    <el-card class="full-table" shadow="hover" style="margin-top: 5px">
      <el-table :data="state.tableData" @selection-change="(val: any[]) => { state.selectData = val; }" style="width: 100%" v-loading="state.tableLoading" tooltip-effect="light" row-key="id" @sort-change="sortChange" border>
        <el-table-column type="selection" width="40" align="center" v-if="auth('incomingorders:batchDelete') || auth('incomingorders:export')" />
        <el-table-column type="index" label="序号" width="55" align="center"/>
        <el-table-column prop='incomingNumber' label='进货单号' show-overflow-tooltip />
        <el-table-column prop='ordersStatus' label='单据状态' show-overflow-tooltip />
        <el-table-column prop='incomingData' label='进货日期' show-overflow-tooltip />
        <el-table-column prop='vendor' label='供应商' :formatter="(row: any) => row.vendorFkDisplayName" show-overflow-tooltip />
        <el-table-column prop='contact' label='联系人' show-overflow-tooltip />
        <el-table-column prop='contactManaer' label='联系方式' show-overflow-tooltip />
        <el-table-column prop='incomingDepId' label='进货部门' :formatter="(row: any) => row.incomingDepFkDisplayName" show-overflow-tooltip />
        <el-table-column prop='incomingPeopleId' label='进货人' :formatter="(row: any) => row.incomingPeopleFkDisplayName" show-overflow-tooltip />
        <el-table-column prop='goodsNum' label='货品数量' show-overflow-tooltip />
        <el-table-column prop='goodsPrices' label='货品单价' show-overflow-tooltip />
        <el-table-column prop='goodsName' label='进货货品' show-overflow-tooltip />
        <el-table-column prop='makeSingleId' label='制单人' :formatter="(row: any) => row.makeSingleFkDisplayName" show-overflow-tooltip />
        <el-table-column prop='depId' label='所在部门' :formatter="(row: any) => row.depFkDisplayName" show-overflow-tooltip />
        <el-table-column prop='incomingDesc' label='备注' show-overflow-tooltip />
        <el-table-column prop='makeData' label='制单时间' show-overflow-tooltip />
        <el-table-column prop='auditTime' label='审核时间' show-overflow-tooltip />
        <el-table-column prop='organizationalStructureId' label='进货明细' show-overflow-tooltip />
        <el-table-column prop='storageDesc' label='上传附件' show-overflow-tooltip />
        <el-table-column prop='operator' label='操作人' :formatter="(row: any) => row.operatorFkDisplayName" show-overflow-tooltip />
        <el-table-column label="修改记录" width="100" align="center" show-overflow-tooltip>
          <template #default="scope">
            <ModifyRecord :data="scope.row" />
          </template>
        </el-table-column>
        <el-table-column label="操作" width="140" align="center" fixed="right" show-overflow-tooltip v-if="auth('incomingorders:update') || auth('incomingorders:delete')">
          <template #default="scope">
            <el-button icon="ele-Edit" size="small" text type="primary" @click="editDialogRef.openDialog(scope.row, '编辑进货订单')" v-auth="'incomingorders:update'"> 编辑 </el-button>
            <el-button icon="ele-Delete" size="small" text type="primary" @click="delIncomingorders(scope.row)" v-auth="'incomingorders:delete'"> 删除 </el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination 
              v-model:currentPage="state.tableParams.page"
              v-model:page-size="state.tableParams.pageSize"
              @size-change="(val: any) => handleQuery({ pageSize: val })"
              @current-change="(val: any) => handleQuery({ page: val })"
              layout="total, sizes, prev, pager, next, jumper"
              :page-sizes="[10, 20, 50, 100, 200, 500]"
              :total="state.tableParams.total"
              size="small"
              background />
      <ImportData ref="importDataRef" :import="incomingordersApi.importData" :download="incomingordersApi.downloadTemplate" v-auth="'incomingorders:import'" @refresh="handleQuery"/>
      <printDialog ref="printDialogRef" :title="'打印进货订单'" @reloadTable="handleQuery" />
      <editDialog ref="editDialogRef" @reloadTable="handleQuery" />
    </el-card>
  </div>
</template>
<style scoped>
:deep(.el-input), :deep(.el-select), :deep(.el-input-number) {
  width: 100%;
}
</style>